package com.cjxt.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjxt.ams.domain.*;
import com.cjxt.ams.exception.RRException;
import com.cjxt.ams.service.MenuService;
import com.cjxt.ams.utils.R;
import com.cjxt.ams.utils.TreeNodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author zhanghuan
 * @version V1.0
 * @Description: 系统菜单控制类
 * @className: MenuController
 * @date 2021/11/23 11:15
 * @company:华勤技术股份有限公司
 * @copyright: Copyright (c) 2021
 */
@RestController
@RequestMapping("/ams/menu")
public class MenuController extends AbstractController {

    @Autowired
    private MenuService menuService;

    /**
     * @param
     * @description: 用户导航菜单
     * @author zhanghuan
     * @date 2021/11/24 9:36
     * @return: com.cjxt.ams.util.R
     */
    @RequestMapping("/nav")
    public List<TreeNodeUser> nav() {
        List<TreeNodeUser> treeNodeUsers = menuService.treeDataUser(getUserId());
        return TreeNodeUtil.transformData2(treeNodeUsers, 0);
    }

    /**
     * @param
     * @description: 新增/修改 菜单树形结构
     * @author zhanghuan
     * @date 2021/12/10 17:09
     * @return: java.util.List<com.cjxt.ams.domain.TreeNode>
     */
    @RequestMapping("/JsonTree")
    public List<TreeNode> JsonTree() {
        List<TreeNode> list = menuService.treeData();
        return TreeNodeUtil.transformData(list, 0);
    }

    /**
     * @param
     * @description: 所有菜单列表
     * @author zhanghuan
     * @date 2021/11/24 14:37
     * @return: java.util.List<com.cjxt.ams.domain.Menu>
     */
    @RequestMapping("/list")
    public R list() {
        List<Menu> menuList = menuService.queryList(menuService.list());

        return R.ok().put("data", menuList);
    }

    /**
     * @param menu
     * @description: 新增
     * @author zhanghuan
     * @date 2021/11/24 14:38
     * @return: com.cjxt.ams.util.R
     */
    @RequestMapping("/save")
    public R add(@RequestBody Menu menu) {
        if (menu.getType() != null) {
            if (menu.getType() == 0) {
                menu.setMenuPid(0);
            }
        }

        QueryWrapper<Menu> qw = new QueryWrapper<>();
        QueryWrapper<Menu> eq = qw.eq("menu_pid", menu.getMenuPid()).eq("menu_name", menu.getMenuName());
        if (menuService.count(eq) > 0) {
            return R.error("此菜单名称已存在，新增失败！");
        }

        try {
            // 数据校验
            verifyForm(menu);
        } catch (RRException e) {
            String msg = e.getMsg();
            return R.error(msg);
        }
        menu.setCreateBy(getUser().getUsername());
        menu.setCreateTime(new Date());
        menuService.save(menu);

        return R.ok();
    }

    /**
     * @param menu
     * @description: 修改
     * @author zhanghuan
     * @date 2021/11/24 14:38
     * @return: com.cjxt.ams.util.R
     */
    @RequestMapping("/update")
    public R update(@RequestBody Menu menu) {
        try {
            verifyForm(menu);
        } catch (RRException e) {
            String msg = e.getMsg();
            return R.error(msg);
        }
        menu.setUpdateBy(getUser().getUsername());
        menu.setUpdateTime(new Date());
        menuService.updateById(menu);
        return R.ok();
    }

    /**
     * @param menuId
     * @description: 删除
     * @author zhanghuan
     * @date 2021/11/24 14:38
     * @return: com.cjxt.ams.util.R
     */
    @RequestMapping("/delete")
    public R delete(Integer menuId) {
        if (menuId <= 5) {
            return R.error("系统菜单，不能删除!!!");
        }

        // 判断是否有子菜单或按钮    查出menuId的子菜单
        List<Menu> menuList = menuService.queryListMenuPid(menuId);
        if (menuList.size() > 0) {
            return R.error("此菜单下还有子菜单或按钮，无法执行删除操作!");
        }

        menuService.delete(menuId);
        return R.ok();
    }

    /**
     * @param menu
     * @description: 验证参数是否正确
     * @author zhanghuan
     * @date 2021/11/24 14:39
     */
    private void verifyForm(Menu menu) {

        if (menu.getType() == null) {
            throw new RRException("菜单类型不能为空!");
        }


        if (menu.getMenuPid() == null) {
            throw new RRException("上级菜单不能为空!");
        }

        // StringUtils.isBlank() 判断某字符串是否为空null  或长度为0 ""  或由空白符"   "  构成
        if (StringUtils.isBlank(menu.getMenuName())) {
            throw new RRException("菜单名称不能为空!");
        }


        // 菜单
        if (menu.getType() == 1) {    // 若为菜单则必须有跳转路径
            if (StringUtils.isBlank(menu.getPath())) {
                throw new RRException("菜单跳转路径Path不能为空!");
            }
        }

        // 上级菜单类型
        Integer parentType = 0;

        if (menu.getMenuPid() != 0) {
            Menu parentMenu = menuService.getById(menu.getMenuPid());
            parentType = parentMenu.getType();
        }

        // 目录0  菜单1
        if (menu.getType() == 0 || menu.getType() == 1) {
            if (parentType != 0) {    // 目录或者菜单的上级只能为目录0
                throw new RRException("上级菜单只能为目录类型!");
            }
        }

        // 按钮2
        if (menu.getType() == 2) {
            if (parentType != 1) {  // 按钮的上级只能为菜单1，不能是目录0
                throw new RRException("上级菜单只能为菜单类型!");
            }
        }
    }
}